﻿using Commonality.ReturnView;
using IService;
using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public abstract class BaseService : IBaseService
    {
      public ISqlSugarClient sqlSugarClient { get; set; }
        public BaseService(ISqlSugarClient sqlSugarClient)
        {
            this.sqlSugarClient = sqlSugarClient;
        }

        #region 根据ID查询实体
        public T IFindEntityForID<T>(int id) where T : class
        {
            return sqlSugarClient.Queryable<T>().InSingle(id);
        }

        public async Task<T> IFindEntityForIDAsync<T>(int id) where T : class
        {
            return await sqlSugarClient.Queryable<T>().InSingleAsync(id);
        }

        public async Task<List<T>> FindQueryForWhere<T>(Expression<Func<T, bool>> expression) where T : class
        {
            return await sqlSugarClient.Queryable<T>().Where(expression).ToListAsync();
        }

        public PagingData<T> QueryPage<T>(Expression<Func<T, bool>> funcWhere, int pageSize, int pageIndex, Expression<Func<T, object>> funcOrderby, bool isAsc = true) where T : class
        {
            int pagecount = 0;
            var page = sqlSugarClient.Queryable<T>().WhereIF(funcWhere != null, funcWhere).OrderByIF(funcOrderby != null, funcOrderby).ToPageList(pageIndex, pageSize, ref pagecount);
            return new PagingData<T>
            {
                DataList = page,
                PageIndex = pageIndex,
                PageSize = pageSize,
                RecordCount = pagecount

            };
        }

        public int Insert<T>(T t) where T : class, new()
        {
            return sqlSugarClient.Insertable(t).ExecuteReturnIdentity();
        }

        public async Task<T> InsertAsync<T>(T t) where T : class, new()
        {
            return await sqlSugarClient.Insertable(t).ExecuteReturnEntityAsync();
        }

        public async Task<bool> InsertList<T>(List<T> tList) where T : class, new()
        {
            var commandInt = await sqlSugarClient.Insertable(tList).ExecuteCommandAsync();
            if (commandInt > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Update<T>(T t) where T : class, new()
        {
            sqlSugarClient.Updateable(t).ExecuteCommand();
        }

        public async Task UpdateAsync<T>(T t) where T : class, new()
        {
            await sqlSugarClient.Updateable(t).ExecuteCommandAsync();
        }

        public void Update<T>(List<T> tList) where T : class, new()
        {
            var result = sqlSugarClient.Updateable(tList).ExecuteCommand();
        }

        public bool Delete<T>(int pId) where T : class, new()
        {

            return sqlSugarClient.Deleteable<T>().In(pId).ExecuteCommandHasChange();
        }

        public async Task<bool> DeleteAsync<T>(int pId) where T : class, new()
        {
            return await sqlSugarClient.Deleteable<T>().In(pId).ExecuteCommandHasChangeAsync();
        }

        public void Delete<T>(T t) where T : class, new()
        {
            sqlSugarClient.Deleteable<T>(t).ExecuteCommandHasChange();
        }

        public void Delete<T>(List<T> tList) where T : class, new()
        {
            sqlSugarClient.Deleteable<T>(tList).ExecuteCommandHasChange();
            //sqlSugarClient.Deleteable(tList).ExecuteCommandHasChange(); //批量删除

        }

        public ISugarQueryable<T> ExcuteQuery<T>(string sql) where T : class, new()
        {
        
            return sqlSugarClient.SqlQueryable<T>(sql);
        }

        public int UpdateableFilter<T>(T t,Expression<Func< T,object>> upColu, Expression<Func<T, bool>> wherCol) where T : class, new()
        {

            var result = sqlSugarClient.Updateable(t).UpdateColumns(upColu).Where(wherCol).ExecuteCommand();
            return result;
        }
        #endregion

    }
}
